<template>
  <div class="forgot-container">
    <div class="forgot-box">
      <div class="forgot-header">
        <div class="logo">
          <img src="../../../assets/vue.svg" />
        </div>
        <h2>重置密码</h2>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef" class="forgot-form">
        <el-form-item prop="email">
          <el-input v-model="form.email" placeholder="邮箱地址">
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="新密码" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="再次确认新密码" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="验证码" style="width: 68%;">
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
          <el-button type="primary" class="code-btn" @click="handleSendCode" :disabled="countdown > 0">
            {{ countdown > 0 ? `${countdown}s后重发` : '获取验证码' }}
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset" class="forgot-button" :disabled="form.code.length !== 6">重置密码</el-button>
        </el-form-item>
      </el-form>
      <div class="back-login" @click="goLogin">
        <el-icon><Back /></el-icon>
        <span>返回登录</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Message, Lock, Back } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { forgetPassword,sendCode } from '@/api/userApi'

const router = useRouter()
const form = reactive({
  email: '',
  password: '',
  confirmPassword: '',
  code: ''
})
const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }, trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}
const formRef = ref()
const countdown = ref(0)
let timer = null

function handleSendCode() {
  if (!form.email) {
    ElMessage.warning('请先输入邮箱')
    return
  }
  sendCode(form.email);
  ElMessage.success('验证码已发送')
  countdown.value = 60
  timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
      timer = null
    }
  }, 1000)
}

function handleReset() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await forgetPassword(form)
      ElMessage.success('密码重置成功')
      router.push('/login')
    } else {
      ElMessage.error('请检查输入')
      return false
    }
  })
}

function goLogin() {
  router.push('/login')
}
</script>

<style scoped>
.forgot-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 120px);
  background-color: #f7f8fa;
}
.forgot-box {
  background-color: white;
  padding: 2.5rem 3rem;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 370px;
  text-align: center;
  position: relative;
}
.forgot-header {
  margin-bottom: 2rem;
}
.logo {
  margin-bottom: 1rem;
}
.forgot-header h2 {
  font-size: 2rem;
  font-weight: 600;
  color: #333;
}
.forgot-form {
  text-align: left;
}
.forgot-button {
  width: 100%;
  padding: 18px 20px;
  font-size: 1rem;
  height: 48px;
  border-radius: 8px;
  background: #6D5BFF;
  border-color: #6D5BFF;
}
.forgot-button:disabled {
  background: #e5e7ef !important;
  border-color: #e5e7ef !important;
  color: #bcbcbc !important;
  cursor: not-allowed;
  box-shadow: none;
}
.forgot-button:hover {
  background: #5a48e3;
  border-color: #5a48e3;
}
.code-btn {
  margin-left: 12px;
  min-width: 100px;
  height: 40px;
  font-size: 0.98rem;
  border-radius: 6px;
  background: #ececff;
  color: #6366f1;
  border: none;
  vertical-align: middle;
  padding: 0 12px;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.code-btn:disabled {
  background: #f0f0f0;
  color: #bbb;
}
.back-login {
  margin-top: 28px;
  margin-left: 10px;
  color: #888;
  display: flex;
  align-items: center;
  justify-content: left;
  cursor: pointer;
  gap: 6px;
  transition: color 0.18s;
}
.back-login span{
  color: #888;
  font-weight: 500;
}
.back-login:hover {
  color: #5a48e3;
}
:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
}
:deep(.el-input__prefix) {
  align-items: center;
  display: flex;
}
:deep(.el-input .el-input__icon) {
  font-size: 16px;
}
:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
}
</style>